<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>BingoTool</title>
    <link th:href="@{/webjars/bootstrap/4.5.3/css/bootstrap.min.css}" rel="stylesheet">
    <script th:src="@{/webjars/jquery/3.5.1/jquery.js}"></script>
    <link rel="shortcut icon" th:href="@{/img/favicon.ico}"/>
    <link rel="bookmark" th:href="@{/img/favicon.ico}"/>
    <style>
        .lblLabel {
            font-size: 0.8rem;
        }
        .lblLabel {
            font-size: 0.8rem;
        }
        .textInput {
            width: 78px;
            font-size: 0.8rem;
        }
        .textInput2 {
            width: 160px;
            font-size: 0.8rem;
        }
    </style>
</head>
<body class="text-center" style="background-color: dimgray; color: white;">
    <div th:replace="common::topNav(activePage='rename')"></div>

    <div class="d-lg-flex mt-2 mb-3">
        <div class="col-lg-3"></div>

        <div class="col-lg-6 text-left">
            <h5 class="text-warning">目录路径</h5>
            <div class="form-group">
                <input class="form-control lblLabel" type="text" id="dirPath">
            </div>

            <div class="custom-control custom-radio">
                <input type="radio" id="byOrgNameSub" value="byOrgNameSub" name="subType" class="custom-control-input" checked="checked">
                <label class="custom-control-label text-warning" for="byOrgNameSub">按文件名截取来重命名</label>
            </div>
            <div class="mt-1 mb-1 text-left">
                <label class="lblLabel ml-2">指定截取字符的索引：( </label>
                <input class="textInput" type="text" id="subStart" placeholder="大于等于 0">
                <label class="lblLabel">, </label>
                <input class="textInput" type="text" id="subEnd" placeholder="默认至结尾">
                <label class="lblLabel"> )</label>
            </div>

            <div class="custom-control custom-radio mt-3">
                <input type="radio" id="byOrder" value="byOrder" name="subType" class="custom-control-input">
                <label class="custom-control-label text-warning" for="byOrder">排序以顺序号命名(选择具体排序规则 ↓)</label>
            </div>
            <div class="text-left ml-2">
                <div class="custom-control custom-radio">
                    <input type="radio" id="orderByCreateTime" value="createTime" name="orderType" class="custom-control-input" checked="checked">
                    <label class="custom-control-label lblLabel" for="orderByCreateTime">根据创建时间升序</label>
                </div>
                <div class="custom-control custom-radio">
                    <input type="radio" id="orderBySize" value="size"  name="orderType" class="custom-control-input">
                    <label class="custom-control-label lblLabel" for="orderBySize">根据存储大小升序</label>
                </div>
                <div class="custom-control custom-radio">
                    <input type="radio" id="orderBySub" value="subStr"  name="orderType" class="custom-control-input">
                    <label class="custom-control-label lblLabel" for="orderBySub">根据文件名的部分字符升序，指定范围：( </label>
                    <input class="textInput" type="text" id="orderSubStart" placeholder="大于等于 0">
                    <label class="lblLabel">, </label>
                    <input class="textInput" type="text" id="orderSubEnd" placeholder="默认至尾部">
                    <label class="lblLabel"> )</label>
                </div>
            </div>

            <div class="mt-3 text-left">
                <label class="ml-2 text-warning">添加前缀</label>
                <input class="textInput2" type="text" id="prefixName">
                <label class="ml-2 text-warning">指定后缀名</label>
                <input class="textInput2" type="text" id="suffixName" placeholder="默认自动识别后缀">
            </div>

            <div class="mt-3 text-left">
                <label class="ml-2 text-warning">跳过前缀为</label>
                <input class="textInput2" type="text" id="skipPrefixName">
                <label class="text-warning">后缀为</label>
                <input class="textInput2" type="text" id="skipSuffixName" value=".torrent">
                <label class="text-warning">的文件</label>
            </div>

            <div class="mt-3 text-left">
                <label class="ml-2 text-warning">仅处理后缀为</label>
                <input class="textInput2" type="text" id="onlySuffixName">
                <label class="text-warning">的文件</label>
            </div>

            <div class="d-inline text-left mt-3">
                <button class="btn btn-sm btn-success" type="button" id="testBtn" onclick="doRename(true)">测试预览</button>
                <button class="btn btn-sm btn-primary ml-2" type="button" id="executeBtn" onclick="doRename(false)">执行</button>
                <label class="text-white-50 lblLabel" id="respMsg"></label>
            </div>
        </div>


        <div class="col-lg-3"></div>
    </div>
</body>
<script>

    function doRename(ifTest) {
        $('#respMsg').text("");
        const dirPath = $('#dirPath').val().trim();
        if(dirPath.length<2 || (!dirPath.includes("/") && !dirPath.includes("\\"))) {
            $('#respMsg').text("目录路径不合法");
            return;
        }

        let numberPattern = /^[0-9]+$/;
        let typeMain, typeOrder;
        let start = null, end = null;
        if($('input:radio[name="subType"]:checked').val()=='byOrgNameSub') {
            // 按名称截取
            typeMain = 1;

            if($('#subStart').val().trim().length<1 || !numberPattern.test($('#subStart').val().trim())) {
                $('#respMsg').text("请填写数字");
                $('#subStart').focus();
                return;
            }
            start = parseInt($('#subStart').val().trim());
            if(start < 0) {
                $('#respMsg').text("开始字符不能小于 0");
                $('#subStart').focus();
                return;
            }

            if($('#subEnd').val().trim().length > 0) {
                if(numberPattern.test($('#subEnd').val().trim())) {
                    end = parseInt($('#subEnd').val().trim());

                    if(end<=start) {
                        $('#respMsg').text("截取的截止字符不能小于等于开始字符");
                        $('#subEnd').focus();
                        return;
                    }
                }else {
                    $('#respMsg').text("请填写数字");
                    $('#subEnd').focus();
                    return;
                }
            }
        }else {
            // 按排序序号
            typeMain = 2;
            typeOrder = $('input:radio[name="orderType"]:checked').val();
            if(typeOrder == 'subStr') {
                if($('#orderSubStart').val().trim().length<1 || !numberPattern.test($('#orderSubStart').val().trim())) {
                    $('#respMsg').text("请填写数字");
                    $('#orderSubStart').focus();
                    return;
                }

                start = parseInt($('#orderSubStart').val().trim());
                if(start < 0) {
                    $('#respMsg').text("开始字符不能小于 0");
                    $('#orderSubStart').focus();
                    return;
                }

                if($('#subEnd').val().trim().length > 0) {
                    if(numberPattern.test($('#orderSubEnd').val().trim())) {
                        end = parseInt($('#orderSubEnd').val().trim());

                        if(end <= start) {
                            $('#respMsg').text("结束字符位置不能小于等于开始字符位置");
                            $('#orderSubEnd').focus();
                            return;
                        }
                    }else {
                        $('#respMsg').text("请填写数字");
                        $('#orderSubEnd').focus();
                        return;
                    }
                }
            }
        }

        let prefix = null;
        if($('#prefixName').val().trim().length > 0) {
            prefix = $('#prefixName').val().trim();
        }
        let suffix = null;
        if($('#suffixName').val().trim().length > 0) {
            suffix = $('#suffixName').val().trim();
            if(!suffix.includes(".")) {
                suffix = "."+suffix;
            }
        }

        let onlySuffix = null;
        if($('#onlySuffixName').val().trim().length > 0) {
            onlySuffix = $('#onlySuffixName').val().trim();
        }

        let skipPrefix = $('#skipPrefixName').val().trim();
        if(skipPrefix.length == 0) {
            skipPrefix = null;
        }

        let skipSuffix = $('#skipSuffixName').val().trim();
        if(skipSuffix.length == 0) {
            skipSuffix = null;
        }

        const jsonParam = JSON.stringify({"dirPath":dirPath, "typeMain":typeMain, "typeOrder":typeOrder,
        "start":start, "end":end, "prefix":prefix, "suffix":suffix, "ifTest":ifTest,
        "skipPrefix":skipPrefix, "skipSuffix":skipSuffix, "onlySuffix":onlySuffix});

        $('#respMsg').text("正在执行......");

        $.ajax({
            url:'execute',
            type:'POST',
            async:false,
            data:jsonParam,
            timeout:60000,
            contentType: "application/json;charset=UTF-8",
            success:function(result){
                if(""+result.message == "success") {
                    if(ifTest) {
                        $('#respMsg').text("重命名后：" + result.data);
                    }else {
                        $('#respMsg').text("执行成功");
                    }
                }else {
                    $('#respMsg').text('执行失败！'+result.message);
                }
            },
            error:function(result){
                $('#respMsg').text('执行失败！'+result.message);
            }
        })
    };

</script>
<script th:src="@{/webjars/bootstrap/4.5.3/js/bootstrap.min.js}"></script>
</html>